home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 81 / IOPROG_81.ISO / tips / PHP / Codice Fiscale / CF.class next >
Encoding:
Text File  |  2004-04-28  |  12.6 KB  |  450 lines

  1. <?php
  2. //************************************************************************
  3. //************************************************************************
  4. //
  5. // Creata da Rosario Sensale
  6. // E.Mail: rs_linux@email.it
  7. // Date: 2004/03/01
  8. // Ver.: 1.00.00
  9. // propietα:
  10. //
  11. //              * SetLastName  (Specifica il Cognome)
  12. //              * SetName      (Specifica il nome)
  13. //              * SetCom       (Specifica il Comune)
  14. //              * SetBornDate  (Specifica la data di nascita)
  15. //              * SetSex       (Specifica il Sesso)
  16. //
  17. // Metodi:
  18. //              * GetLastName  (Restituisce il Cognome)
  19. //              * GetName      (Restituisce il nome)
  20. //              * GetCom       (Restituisce il Comune)
  21. //              * GetBornDate  (Restituisce la data di nascita)
  22. //              * GetSex       (Restituisce il Sesso)
  23. //              * Create_CF    (Crea il codice fiscale)
  24. //              * Check_CF     (Controlla il codice fiscale
  25. //                             (restituisce 1 se Φ corretto)
  26. //
  27. // Ritorno della funzione Create_CF:
  28. // In caso di errore la funzione ritorna:
  29. //  -1      -> Cognome non presente
  30. //  -2      -> Nome non presente
  31. //  -3      -> Data di nascita non presente
  32. //  -4      -> Sesso non presente
  33. //  -5      -> Sesso non valido (diverso da M,F);
  34. //  -6      -> Comune non specificato
  35. //  -7      -> Comune non presente in archivio
  36. //  -8      -> Errore nella data
  37. //  -9      -> I codifi fiscali sono diversi
  38. //************************************************************************
  39. //************************************************************************
  40.  
  41. class Class_CF{
  42.     var $cognome;
  43.     var $nome;
  44.     var $sesso;
  45.     var $data_nascita;
  46.     var $comune;
  47.  
  48.     //Class construct
  49.     function __construct(){
  50.         $this->comune='';
  51.         $this->data_nascita='';
  52.         $this->sesso='';
  53.         $this->nome='';
  54.         $this->cognome='';
  55.         $this->codice_comune='';
  56.     }
  57.  
  58.     //Class destruct
  59.     function __destruct(){
  60.     }
  61.  
  62.     //Set il cognome
  63.     function SetLastName($object){
  64.         $this->cognome=$object;
  65.     }
  66.  
  67.     //Set il nome
  68.     function SetName($object){
  69.         $this->nome=$object;
  70.     }
  71.  
  72.     //Setta la data di nascita
  73.     function SetBornDate($object){
  74.         $this->data_nascita=$object;
  75.     }
  76.  
  77.     //Setta il sesso
  78.     function SetSex($object){
  79.         $this->sesso=$object;
  80.     }
  81.  
  82.     //Setta il comune
  83.     function SetCom($object){
  84.         $this->comune=$object;
  85.     }
  86.  
  87.  
  88.     //Restituisce il cognome
  89.     function GetLastName(){
  90.         return $this->cognome;
  91.     }
  92.  
  93.     //Restituisce il nome
  94.     function GetName(){
  95.         return $this->nome;
  96.     }
  97.  
  98.     //Restituisce la data di nascita
  99.     function GetBornDate(){
  100.         return  $this->data_nascita;
  101.     }
  102.  
  103.     //Restituisce il comune
  104.     function GetCom(){
  105.         return $this->comune;
  106.     }
  107.  
  108.     //Restituisce il sesso
  109.     function GetSex(){
  110.         return $this->sesso;
  111.     }
  112.  
  113.  
  114.     //funzione utile x controllare il sesso
  115.     function CheckSesso(&$Sesso){
  116.     $Sesso=strtoupper($Sesso);
  117.  
  118.     if (($Sesso != 'M') && ($Sesso != 'F')){
  119.         return 0;
  120.     }elseif (strLen($Sesso) > 1){
  121.         return 0;
  122.     }else{
  123.         return 1;
  124.     }
  125.     }
  126.  
  127.     //Restituisce il codice del comune
  128.     function Comune($Comune){
  129.     $codice_comune='0';
  130.     $fp = fopen("Comuni.csv","r");
  131.     while (  ($stringa = fgets($fp,4096)) && ($codice_comune=="0")   ){
  132.                $campi=explode(";",$stringa);
  133.                $campi[2]=trim($campi[2]);
  134.                if (strCmp($campi[2],$Comune) == 0){
  135.                     $codice_comune=$campi[0];
  136.                }
  137.     }
  138.     fclose($fp);
  139.     return $codice_comune;
  140.     }
  141.  
  142.  
  143.     // Data una stringa restituisce quest'ultima senza
  144.     //le vocali CASA -> CS
  145.     function OnlyCons($strStringa) {
  146.     $Max=strLen($strStringa);
  147.     $Temp='';
  148.     $Char='';
  149.  
  150.     if ($Max > 0){
  151.         for ($i=0;$i<$Max;$i++){
  152.             $Char=substr($strStringa,$i,1);
  153.             if (($Char != ' ') && ($Char != 'A') && ($Char != 'E') && ($Char != 'I') && ($Char != 'O') && ($Char != 'U')){
  154.                 $Temp .= $Char;
  155.             }
  156.         }
  157.         return trim($Temp);
  158.     }else{
  159.         return '';
  160.     }
  161.     }
  162.  
  163.     //Data una stringa restituisce quest'ultima senza
  164.     //le consolanti CASA -> AA
  165.     function OnlyVoc($strStringa) {
  166.     $Max=strLen($strStringa);
  167.     $Temp='';
  168.     $Char='';
  169.  
  170.     if ($Max > 0){
  171.         for ($i=0;$i<$Max;$i++){
  172.             $Char=substr($strStringa,$i,1);
  173.             if ((($Char == 'A') || ($Char == 'E') || ($Char == 'I') || ($Char == 'O') || ($Char == 'U')) && ($Char != ' ')){
  174.                 $Temp .= $Char;
  175.             }
  176.         }
  177.         return $Temp;
  178.     }else{
  179.         return '';
  180.     }
  181.     }
  182.  
  183.     //Elimina tutte le lettere accentate
  184.     //Esempio MACCARO' -> MACCARO
  185.     function NoAccento($strStringa){
  186.     $Accentate="└╚╔╠╥┘αΦΘ∞≥∙";
  187.     $No_Accentate="AEEIOUAEEIOU";
  188.     $Max=strLen($strStringa);
  189.     if ($Max > 0){
  190.         for ($i=0;$i<$Max;$i++){
  191.             $pos=strpos($Accentate,substr($strStringa,$i,1));
  192.             if ($pos){
  193.                 $strStringa[$i]=substr($No_Accentate,$pos,1);
  194.             }
  195.         }
  196.     }
  197.         return $strStringa;
  198.     }
  199.  
  200.     //Restituisce il Cognome nella forma corretta
  201.     function GetLastname($strStringa){
  202.     $Consol=$this->OnlyCons($strStringa);
  203.     $Max=strLen($Consol);
  204.  
  205.     if ($Max > 3) {
  206.         return substr($Consol,0,3);
  207.     }elseif ($Max < 3){
  208.         $Vocali=OnlyVoc($strStringa);
  209.         return $Consol . substr($Vocali,0,abs(3-$Max));
  210.     }elseif ($Max == 3){
  211.         return $Consol;
  212.     }
  213.     }
  214.  
  215.     //Restituisce il nome nella forma corretta
  216.     function GetName($strStringa){
  217.     $Consol=$this->OnlyCons($strStringa);
  218.     $Max=strLen($Consol);
  219.  
  220.     if ($Max > 3) {
  221.         return $Consol[0] . $Consol[2] . $Consol[3];
  222.     }elseif ($Max < 3){
  223.         $Vocali=OnlyVoc($strStringa);
  224.         return $Consol . substr($Vocali,0,abs(3-$Max));
  225.     }elseif ($Max == 3){
  226.         return $Consol;
  227.     }
  228.     }
  229.  
  230.  
  231.     //controllo del giorno
  232.     function CreateDay($giorno,$Sesso){
  233.     if ($Sesso == 'M'){
  234.         if (strlen($giorno)<2){$giorno= "0" . $giorno;} //4 -> 04
  235.     }elseif ($Sesso == 'F'){
  236.         $giorno=intval($giorno)+40;
  237.     }
  238.     return $giorno;
  239.     }
  240.  
  241.  
  242.     //controllo della carattere di controllo
  243.     function CheckControlKey($codice_fiscale){
  244.     //A
  245.     $Matrice_Controllo[65][0] = 1;
  246.     $Matrice_Controllo[65][1] = 0;
  247.     // "B"
  248.     $Matrice_Controllo[66][0] = 0;
  249.     $Matrice_Controllo[66][1] = 1;
  250.     // "C"
  251.     $Matrice_Controllo[67][0] = 5;
  252.     $Matrice_Controllo[67][1] = 2;
  253.     // "D"
  254.     $Matrice_Controllo[68][0] = 7;
  255.     $Matrice_Controllo[68][1] = 3;
  256.     // "E"
  257.     $Matrice_Controllo[69][0] = 9;
  258.     $Matrice_Controllo[69][1] = 4;
  259.     // "F"
  260.     $Matrice_Controllo[70][0] = 13;
  261.     $Matrice_Controllo[70][1] = 5;
  262.     // "G"
  263.     $Matrice_Controllo[71][0] = 15;
  264.     $Matrice_Controllo[71][1] = 6;
  265.     // "H"
  266.     $Matrice_Controllo[72][0] = 17;
  267.     $Matrice_Controllo[72][1] = 7;
  268.     // "I"
  269.     $Matrice_Controllo[73][0] = 19;
  270.     $Matrice_Controllo[73][1] = 8;
  271.     //J"
  272.     $Matrice_Controllo[74][0] = 21;
  273.     $Matrice_Controllo[74][1] = 9;
  274.     // "K"
  275.     $Matrice_Controllo[75][0] = 2;
  276.     $Matrice_Controllo[75][1] = 10;
  277.     // "L"
  278.     $Matrice_Controllo[76][0] = 4;
  279.     $Matrice_Controllo[76][1] = 11;
  280.     // "M"
  281.     $Matrice_Controllo[77][0] = 18;
  282.     $Matrice_Controllo[77][1] = 12;
  283.     // "N"
  284.     $Matrice_Controllo[78][0] = 20;
  285.     $Matrice_Controllo[78][1] = 13;
  286.     // "O"
  287.     $Matrice_Controllo[79][0] = 11;
  288.     $Matrice_Controllo[79][1] = 14;
  289.     // "P"
  290.     $Matrice_Controllo[80][0] = 3;
  291.     $Matrice_Controllo[80][1] = 15;
  292.     // "Q"
  293.     $Matrice_Controllo[81][0] = 6;
  294.     $Matrice_Controllo[81][1] = 16;
  295.     // "R"
  296.     $Matrice_Controllo[82][0] = 8;
  297.     $Matrice_Controllo[82][1] = 17;
  298.     // "S"
  299.     $Matrice_Controllo[83][0] = 12;
  300.     $Matrice_Controllo[83][1] = 18;
  301.     // "T"
  302.     $Matrice_Controllo[84][0] = 14;
  303.     $Matrice_Controllo[84][1] = 19;
  304.     // "U"
  305.     $Matrice_Controllo[85][0] = 16;
  306.     $Matrice_Controllo[85][1] = 20;
  307.     // "V"
  308.     $Matrice_Controllo[86][0] = 10;
  309.     $Matrice_Controllo[86][1] = 21;
  310.     // "W"
  311.     $Matrice_Controllo[87][0] = 22;
  312.     $Matrice_Controllo[87][1] = 22;
  313.     // "X"
  314.     $Matrice_Controllo[88][0] = 25;
  315.     $Matrice_Controllo[88][1] = 23;
  316.     // "Y"
  317.     $Matrice_Controllo[89][0] = 24;
  318.     $Matrice_Controllo[89][1] = 24;
  319.     // "Z"
  320.     $Matrice_Controllo[90][0] = 23;
  321.     $Matrice_Controllo[90][1] = 25;
  322.     // "0"
  323.     $Matrice_Controllo[48][0] = 1;
  324.     $Matrice_Controllo[48][1] = 0;
  325.     // "1"
  326.     $Matrice_Controllo[49][0] = 0;
  327.     $Matrice_Controllo[49][1] = 1;
  328.     // "2"
  329.     $Matrice_Controllo[50][0] = 5;
  330.     $Matrice_Controllo[50][1] = 2;
  331.     // "3"
  332.     $Matrice_Controllo[51][0] = 7;
  333.     $Matrice_Controllo[51][1] = 3;
  334.     // "4"
  335.     $Matrice_Controllo[52][0] = 9;
  336.     $Matrice_Controllo[52][1] = 4;
  337.     // "5"
  338.     $Matrice_Controllo[53][0] = 13;
  339.     $Matrice_Controllo[53][1] = 5;
  340.     // "6"
  341.     $Matrice_Controllo[54][0] = 15;
  342.     $Matrice_Controllo[54][1] = 6;
  343.     // "7"
  344.     $Matrice_Controllo[55][0] = 17;
  345.     $Matrice_Controllo[55][1] = 7;
  346.     // "8"
  347.     $Matrice_Controllo[56][0] = 19;
  348.     $Matrice_Controllo[56][1] = 8;
  349.     // "9"
  350.     $Matrice_Controllo[57][0] = 21;
  351.     $Matrice_Controllo[57][1] = 9;
  352.  
  353.     //calcola carattere di controllo
  354.     $codcontrollo = 0;
  355.     //scorre $codice fino ad ora calcolato
  356.     For ($i= 0;$i<strLen($codice_fiscale);$i++){
  357.         $codcontrollo += $Matrice_Controllo[ord(substr($codice_fiscale, $i, 1))][$i % 2];
  358.     }
  359.         return chr(65 + ($codcontrollo % 26));
  360.  
  361.     }
  362.  
  363.     //controlla la data
  364.     //restituisce TRUE se la data Φ esatta
  365.     function CheckData($giorno,$mese,$anno){
  366.         if (checkdate(intval($mese),intval($giorno),intval($anno)) == true){
  367.             return true;
  368.         }else{
  369.             return false;
  370.         }
  371.     }
  372.  
  373.  
  374.     //Crea il codice fiscale
  375.     function Create_CF(){
  376.         if (!isset($this->cognome)){
  377.             return -1;
  378.         }elseif (!isset($this->nome)){
  379.             return -2;
  380.         }elseif (!isset($this->data_nascita)){
  381.             return -3;
  382.         }elseif (!isset($this->sesso)){
  383.             return -4;
  384.         }elseif ($this->CheckSesso($this->sesso) == 0){
  385.             return -5;
  386.         }elseif (!isset($this->comune)){
  387.             return -6;
  388.         }else{
  389.             $Mesi = "ABCDEHLMPRST"; //lettere x corrispondenza mesi
  390.             
  391.             //prendo il codice del comune
  392.             $this->comune=strtoupper($this->comune);
  393.             $this->codice_comune=$this->Comune($this->comune);
  394.             $this->cognome=strtoupper($this->cognome);
  395.             $this->nome=strtoupper($this->nome);
  396.             
  397.             if ($this->codice_comune == '0'){
  398.                 return -7;
  399.             }else{  //si puo' proseguire
  400.             //elimino le accentate
  401.  
  402.                 
  403.                 $this->cognome=$this->NoAccento($this->cognome);
  404.                 $this->nome=$this->NoAccento($this->nome);
  405.                 $codice_fiscale  = $this->GetLastname($this->cognome);
  406.                 $codice_fiscale .= $this->GetName($this->nome);
  407.  
  408.                 //controllo la data
  409.                 $data=explode("/",$this->data_nascita);
  410.                 if (sizeof($data)!=3){
  411.                     return -8;
  412.                 }elseif ($this->CheckData($data[0],$data[1],$data[2]) == false) {
  413.                     return -8;
  414.                 } else {
  415.                     $anno=$data[2];
  416.                     $mese=$data[1];
  417.                     $giorno=$data[0];
  418.                     if (strlen($anno)<2){$anno= "0" . $anno;} //4 -> 04
  419.                     if (strlen($anno)==4){$anno=substr($anno,2,2);} //2004 -> 04
  420.  
  421.                     $codice_fiscale .= $anno; //Aggiunta dell'anno
  422.                     $codice_fiscale .= substr($Mesi,($mese-1),1); //Mese
  423.                 }
  424.                 //codifica del giorno di nascita
  425.                 $codice_fiscale .= $this->CreateDay($giorno,$this->sesso);
  426.                 $codice_fiscale .= $this->codice_comune;
  427.                 $codice_fiscale .= $this->CheckControlKey($codice_fiscale);
  428.                 
  429.                 return $codice_fiscale;
  430.             }
  431.         }
  432.     }
  433.  
  434.  
  435.     //Controlla che il codice fiscale passato come
  436.     //argomento Φ corretto
  437.     //restituisce 1 se Φ corretto
  438.     function Check_CF($cf){
  439.     $codice_fiscale=$this->Create_CF();
  440.     if (strCmp($cf,$codice_fiscale) != 0){
  441.         return -9;
  442.     }else{
  443.         return 1;
  444.     }
  445.     }
  446.  
  447. }
  448. ?>
  449.  
  450.